1   /*                        __    __  __  __    __  ___
2    *                       \  \  /  /    \  \  /  /  __/
3    *                        \  \/  /  /\  \  \/  /  /
4    *                         \____/__/  \__\____/__/.ɪᴏ
5    * ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
6    */
7   package io.vavr.collection;
8   
9   import io.vavr.Value;
10  import io.vavr.Tuple2;
11  import io.vavr.control.Option;
12  import org.junit.Test;
13  
14  import java.math.BigDecimal;
15  import java.util.ArrayList;
16  import java.util.function.Function;
17  import java.util.function.Supplier;
18  import java.util.stream.Collector;
19  
20  public class ArrayTest extends AbstractIndexedSeqTest {
21  
22      @Override
23      protected <T> Collector<T, ArrayList<T>, ? extends Seq<T>> collector() {
24          return Array.collector();
25      }
26  
27      @Override
28      protected <T> Array<T> empty() {
29          return Array.empty();
30      }
31  
32      @Override
33      protected <T> Array<T> of(T element) {
34          return Array.of(element);
35      }
36  
37      @SuppressWarnings("varargs")
38      @SafeVarargs
39      @Override
40      protected final <T> Array<T> of(T... elements) {
41          return Array.of(elements);
42      }
43  
44      @Override
45      protected <T> Array<T> ofAll(Iterable<? extends T> elements) {
46          return Array.ofAll(elements);
47      }
48  
49      @Override
50      protected <T extends Comparable<? super T>> Array<T> ofJavaStream(java.util.stream.Stream<? extends T> javaStream) {
51          return Array.ofAll(javaStream);
52      }
53  
54      @Override
55      protected Array<Boolean> ofAll(boolean... elements) {
56          return Array.ofAll(elements);
57      }
58  
59      @Override
60      protected Array<Byte> ofAll(byte... elements) {
61          return Array.ofAll(elements);
62      }
63  
64      @Override
65      protected Array<Character> ofAll(char... elements) {
66          return Array.ofAll(elements);
67      }
68  
69      @Override
70      protected Array<Double> ofAll(double... elements) {
71          return Array.ofAll(elements);
72      }
73  
74      @Override
75      protected Array<Float> ofAll(float... elements) {
76          return Array.ofAll(elements);
77      }
78  
79      @Override
80      protected Array<Integer> ofAll(int... elements) {
81          return Array.ofAll(elements);
82      }
83  
84      @Override
85      protected Array<Long> ofAll(long... elements) {
86          return Array.ofAll(elements);
87      }
88  
89      @Override
90      protected Array<Short> ofAll(short... elements) {
91          return Array.ofAll(elements);
92      }
93  
94      @Override
95      protected <T> Array<T> tabulate(int n, Function<? super Integer, ? extends T> f) {
96          return Array.tabulate(n, f);
97      }
98  
99      @Override
100     protected <T> Array<T> fill(int n, Supplier<? extends T> s) {
101         return Array.fill(n, s);
102     }
103 
104     @Override
105     protected Array<Character> range(char from, char toExclusive) {
106         return Array.range(from, toExclusive);
107     }
108 
109     @Override
110     protected Array<Character> rangeBy(char from, char toExclusive, int step) {
111         return Array.rangeBy(from, toExclusive, step);
112     }
113 
114     @Override
115     protected Array<Double> rangeBy(double from, double toExclusive, double step) {
116         return Array.rangeBy(from, toExclusive, step);
117     }
118 
119     @Override
120     protected Array<Integer> range(int from, int toExclusive) {
121         return Array.range(from, toExclusive);
122     }
123 
124     @Override
125     protected Array<Integer> rangeBy(int from, int toExclusive, int step) {
126         return Array.rangeBy(from, toExclusive, step);
127     }
128 
129     @Override
130     protected Array<Long> range(long from, long toExclusive) {
131         return Array.range(from, toExclusive);
132     }
133 
134     @Override
135     protected Array<Long> rangeBy(long from, long toExclusive, long step) {
136         return Array.rangeBy(from, toExclusive, step);
137     }
138 
139     @Override
140     protected Array<Character> rangeClosed(char from, char toInclusive) {
141         return Array.rangeClosed(from, toInclusive);
142     }
143 
144     @Override
145     protected Array<Character> rangeClosedBy(char from, char toInclusive, int step) {
146         return Array.rangeClosedBy(from, toInclusive, step);
147     }
148 
149     @Override
150     protected Array<Double> rangeClosedBy(double from, double toInclusive, double step) {
151         return Array.rangeClosedBy(from, toInclusive, step);
152     }
153 
154     @Override
155     protected Array<Integer> rangeClosed(int from, int toInclusive) {
156         return Array.rangeClosed(from, toInclusive);
157     }
158 
159     @Override
160     protected Array<Integer> rangeClosedBy(int from, int toInclusive, int step) {
161         return Array.rangeClosedBy(from, toInclusive, step);
162     }
163 
164     @Override
165     protected Array<Long> rangeClosed(long from, long toInclusive) {
166         return Array.rangeClosed(from, toInclusive);
167     }
168 
169     @Override
170     protected Array<Long> rangeClosedBy(long from, long toInclusive, long step) {
171         return Array.rangeClosedBy(from, toInclusive, step);
172     }
173 
174     @Override
175     @SuppressWarnings("unchecked")
176     protected <T> Array<Array<T>> transpose(Seq<? extends Seq<T>> rows) {
177         return Array.transpose((Array<Array<T>>) rows);
178     }
179 
180     @Override
181     protected int getPeekNonNilPerformingAnAction() {
182         return 1;
183     }
184 
185     @Override
186     protected boolean useIsEqualToInsteadOfIsSameAs() {
187         return false;
188     }
189 
190     // -- static narrow
191 
192     @Test
193     public void shouldNarrowArray() {
194         final Array<Double> doubles = of(1.0d);
195         final Array<Number> numbers = Array.narrow(doubles);
196         final int actual = numbers.append(new BigDecimal("2.0")).sum().intValue();
197         assertThat(actual).isEqualTo(3);
198     }
199 
200     // -- transform()
201 
202     @Test
203     public void shouldTransform() {
204         String transformed = of(42).transform(v -> String.valueOf(v.get()));
205         assertThat(transformed).isEqualTo("42");
206     }
207 
208     // -- unfold
209 
210     @Test
211     public void shouldUnfoldRightToEmpty() {
212         assertThat(Array.unfoldRight(0, x -> Option.none())).isEqualTo(empty());
213     }
214 
215     @Test
216     public void shouldUnfoldRightSimpleArray() {
217         assertThat(
218                 Array.unfoldRight(10, x -> x == 0
219                                            ? Option.none()
220                                            : Option.of(new Tuple2<>(x, x - 1))))
221                 .isEqualTo(of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
222     }
223 
224     @Test
225     public void shouldUnfoldLeftToEmpty() {
226         assertThat(Array.unfoldLeft(0, x -> Option.none())).isEqualTo(empty());
227     }
228 
229     @Test
230     public void shouldUnfoldLeftSimpleArray() {
231         assertThat(
232                 Array.unfoldLeft(10, x -> x == 0
233                                           ? Option.none()
234                                           : Option.of(new Tuple2<>(x - 1, x))))
235                 .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
236     }
237 
238     @Test
239     public void shouldUnfoldToEmpty() {
240         assertThat(Array.unfold(0, x -> Option.none())).isEqualTo(empty());
241     }
242 
243     @Test
244     public void shouldUnfoldSimpleArray() {
245         assertThat(
246                 Array.unfold(10, x -> x == 0
247                                       ? Option.none()
248                                       : Option.of(new Tuple2<>(x - 1, x))))
249                 .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
250     }
251 
252     // -- toString
253 
254     @Test
255     public void shouldStringifyNil() {
256         assertThat(empty().toString()).isEqualTo("Array()");
257     }
258 
259     @Test
260     public void shouldStringifyNonNil() {
261         assertThat(of(1, 2, 3).toString()).isEqualTo("Array(1, 2, 3)");
262     }
263 
264     // -- toArray
265 
266     @Test
267     public void shouldReturnSelfOnConvertToArray() {
268         Value<Integer> value = of(1, 2, 3);
269         assertThat(value.toArray()).isSameAs(value);
270     }
271 }